<?php
$catid		= empty($_GET['catid']) ? 0 : intval($_GET['catid']);
$recommend	= empty($_GET['recommend']) ? '' : trim($_GET['recommend']);

$orderby = get_major_key(array('rank' => 'DESC', 'id' => 'DESC'), null, 'id');

$oGoodscat = new Goodscat();
$categorys = cache_read('goods_categorys.php');

// 搜索参数
if($catid > 0){
	$childs = $oGoodscat->get_category_list($_GET['catid']);
	if(!empty($childs)){
		$childs = array_keys($childs);
		$childs[] = $_GET['catid'];
		$_GET['childs'] = implode(',', $childs);
		unset($_GET['catid']);
	}
}

$oGoods = new Goods();

if(!empty($_GET['btnExport'])){	// 导出
	$list = $oGoods->get_list('*', $_GET, $orderby, 1, 65536, $offset = 0);
	if(!empty($list['list']) && is_array($list['list'])){
		$data = array();
		foreach($list['list'] as $row){
			$row['category'] = $categorys['category_list'][$row['catid']]['name'];
			$row['fweight'] = format_weight_out($row['weight'], $row['weightunit']);
			$row['recommend'] = $row['recommend'] == '' || $row['recommend'] == 'normal' ? '--' : Statics::$recommend_type_arr[$row['recommend']];
			$row['show'] = $row['show'] == 1 ? '显示' : '-';
			$row['date'] = fdate($row['addtime']);
			$row['time'] = ftime($row['addtime']);
			$data[] = $row;
		}
		$headers = array(
			'id'		=> array('title' => 'ID',	'width' => '6'),
			'name'		=> array('title' => '商品', 'width' => '50', 'wrap' => true),
			'category'	=> array('title' => '分类', 'width' => '12.5'),
			'fweight'	=> array('title' => '重量（Kg）', 'width' => '12.5'),
			'price'		=> array('title' => '价格（￥）', 'width' => '12.5'),
			'quantity'	=> array('title' => '数量', 'width' => '8'),
			'recommend'	=> array('title' => '推荐', 'width' => '8'),
			'summary'	=> array('title' => '摘要', 'width' => '50', 'wrap' => true),
			'hits'		=> array('title' => '点击', 'width' => '6'),
			'date'		=> array('title' => '日期', 'width' => '12.5'),
			'time'		=> array('title' => '时间', 'width' => '18'),
			'cover'		=> array('title' => '封面图片', 'width' => 'auto'),
		);
		if($list['total'] > 5000){	// 导出超过5000时用CSV
			export_csv($data, $headers, '_E_商品_'.date('Ymd_His').'.csv');
		}else{
			$oExcel = new Excel();
			$oExcel->export($headers, $data, '_E_商品_'.date('Ymd_His').'.xlsx');
		}
	}
	exit;
}

#

$list = $oGoods->get_list('*', $_GET, $orderby, $__page, $__pagesize);
if(!empty($list['list']) && is_array($list['list'])){

	// 主键排序字段
	make_major_key_options(array(
		'id'		=> '添加时间',
		'price'		=> '价格',
		'weight'	=> '重量',
		'quantity'	=> '库存',
		
		'orderby_views'		=> '查看',
		'orderby_sales'		=> '销量',
	));

	$_recommend_type_arr = Statics::$recommend_type_arr;
	$_recommend_type_arr[0] = '';
	foreach($list['list'] as $key => $row){
		$row['category'] = $categorys['category_list'][$row['catid']]['name'];
		$row['fweight'] = format_weight_out($row['weight'], $row['weightunit']);
		$row['recommend_options'] = make_options($_recommend_type_arr, false, $row['recommend']);
		if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
			if(empty($row['filename'])){
				$row['url'] = PUB_DIR . 'goods/' . time_to_path($row['addtime']) . '/' . $row['id'] . '.html';
			}else{
				$row['url'] = PUB_DIR . 'goods/' . $row['filename'] . '.html';
			}
		}else{
			$row['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$row['id']}";
		}
		$row['addtime'] = ftime($row['addtime']);
		$row['updatetime'] = ftime($row['updatetime']);
		$list['list'][$key] = $row;
	}
	$vars['list'] = $list['list'];
	$vars['pagelist'] = $list['pagelist'];
}

// 保持搜索参数
$vars['recommend_options'] = make_options(Statics::$recommend_type_arr, true, $recommend);
$vars['category_tree'] = $oGoodscat->get_category_tree_ul(); // 取分类树
if($catid > 0){
	$_GET['catid'] = $catid;
	$vars['category_name'] = $categorys['category_list'][$catid]['name'];
}else{
	$vars['category_name'] = $__l['root'];
}
?>